package knowledge

import (
	"github.com/gin-gonic/gin"
	"smartLearn/models"
	"smartLearn/modules/response"
	"strconv"
	"strings"
)

func GetList(c *gin.Context) {
	subjectId,err:=strconv.Atoi(c.Query("subjectId"))
	if err!=nil{
		response.ShowError(c,err.Error())
		return
	}
	//读取相关的阶段信息
	levelIds:=c.Query("levelIds")

	knowledgeModel := models.SystemKnowledge{};
	knowledgeInfoList,err :=knowledgeModel.GetBySubjectAndLevel(subjectId,levelIds)
	if err!=nil{
		response.ShowError(c,err.Error())
		return
	}
	var data = make(map[string]interface{}, 0)

	var list []interface{}
	var rootNode = make(map[string]interface{}, 0)
	knowledgeMap:=getKnowledgeMap(knowledgeInfoList)
	rootNode["id"]=0
	rootNode["name"]="所有知识点"
	myChildren:=getFromKnowledgeInfoList(knowledgeMap,knowledgeInfoList,0)

	if len(myChildren)>0 {
		rootNode["children"]=myChildren
	}

	list = append(list, rootNode)

	data["list"]=list
	response.ShowData(c,data)

}
func getKnowledgeMap(kList  [] models.KnowledgeFlatLevelInfo)  map[int]string{
	var kMap = make(map[int]string, 0)
	//kMap的key是parentId,value是childid (有多个children的用逗号隔开)
	for i,k := range kList {
		parentId:=k.Parentid
		if _,ok := kMap[parentId]; ok {
			//存在
			kMap[parentId]=kMap[parentId] +","+strconv.Itoa(i)
		}else{
			kMap[parentId]=strconv.Itoa(i)
		}

	}
	return kMap
}
func getFromKnowledgeInfoList(kMap map[int]string,kList []models.KnowledgeFlatLevelInfo,parentId int) []interface{}{
	var children []interface{}

	var childIndexs []string

	if _,ok := kMap[parentId]; ok {
		//存在
		childIndexs=strings.Split(kMap[parentId],",")

		for _,childIndex := range childIndexs {

			index,err:=strconv.Atoi(childIndex)
			if err!=nil {
				return children
			}
			var node = make(map[string]interface{}, 0)
			node["id"]=kList[index].Id
			node["name"]=kList[index].Name
			node["levelIds"]=kList[index].LevelIds
			myChildren:=getFromKnowledgeInfoList(kMap,kList,kList[index].Id)
			if len(myChildren)>0 {
				node["children"]=myChildren
			}

			children=append(children,node)
		}

	}

	return children

}

func Add(c *gin.Context) {
	name:=c.Query("name")
	comment:=c.Query("comment")
	subjectId,err:=strconv.Atoi(c.Query("subjectId"))
	if err!=nil{
		response.ShowError(c,err.Error())
		return
	}
	parentId,err:=strconv.Atoi(c.Query("parentId"))
	if err!=nil{
		response.ShowError(c,err.Error())
		return
	}
	//读取相关的阶段信息
	levelIds:=strings.Split(c.Query("levelIds"),",")

	levelIdsArr := make([]int, len(levelIds))


	for i, v := range levelIds {
		levelIdsArr[i], err = strconv.Atoi(v)
		if err != nil {
			continue //如果传入非数组，那么忽略
		}
	}

	knowledgeModel := models.SystemKnowledge{};
	knowledgeModel.Name=name
	knowledgeModel.Comment=comment
	knowledgeModel.SubjectId=subjectId
	knowledgeModel.Parentid=parentId
	newid,err:=knowledgeModel.Add(levelIdsArr)
	if err!=nil{
		response.ShowError(c,err.Error())
		return
	}
	var data = make(map[string]interface{}, 0)
	data["id"]=newid
	response.ShowData(c,data)

}

func GetById(c *gin.Context) {
	knowledgeId,err:=strconv.Atoi(c.Query("id"))
	if err!=nil{
		response.ShowError(c,err.Error())
		return
	}
	knowledgeModel := models.SystemKnowledge{};
	knowledgeModel.Id=knowledgeId
	knowledgeModel,err=knowledgeModel.GetById(knowledgeId)

	if err!=nil{
		response.ShowError(c,err.Error())
		return
	}

	var data = make(map[string]interface{}, 0)

	data["comment"]=knowledgeModel.Comment

	response.ShowData(c,data)
}

func Update(c *gin.Context) {
	id,err:=strconv.Atoi(c.Query("id"))
	if err!=nil{
		response.ShowError(c,err.Error())
		return
	}
	name:=c.Query("name")
	comment:=c.Query("comment")

	//读取相关的阶段信息
	levelIds:=strings.Split(c.Query("levelIds"),",")

	levelIdsArr := make([]int, len(levelIds))


	for i, v := range levelIds {
		levelIdsArr[i], err = strconv.Atoi(v)
		if err != nil {
			continue //如果传入非数组，那么忽略
		}
	}

	knowledgeModel := models.SystemKnowledge{};
	knowledgeModel.Id=id
	knowledgeModel.Name=name
	knowledgeModel.Comment=comment
	//只能更新知识点的名称，说明还有对应的阶段
	_,err=knowledgeModel.Update(levelIdsArr)
	if err!=nil{
		response.ShowError(c,err.Error())
		return
	}
	response.ShowSuccess(c,"更新知识点成功" )

}

func Delete(c *gin.Context) {

	knowledgeModel := models.SystemKnowledge{};

	var err error
	knowledgeModel.Id,err =strconv.Atoi(c.Query("id"))
	if err!=nil{
		response.ShowError(c,err.Error())
		return
	}

	err=knowledgeModel.Delete()
	if err!=nil{
		response.ShowError(c,err.Error())
		return
	}
	response.ShowSuccess(c,"删除知识点成功" )

}